<?php
namespace console\controllers;

use Yii;
use common\models\Youdao;

/**
 *
 */
class TaskController extends \yii\console\Controller{

	public function actionSyncWord(){
        $db = Yii::$app->db;
        $sql = "SELECT id, answer FROM {{%knowledge}}";

        $knowledges = $db->createCommand($sql)->queryAll();

        //print_r($knowledges);
        foreach($knowledges as $knowledge){
            $word = Youdao::getWordByCopyWeb($knowledge["answer"]);

            echo $word."\n";

            if(!empty($word)){
                $sql = "UPDATE {{%knowledge}} SET `word`='".$word."' WHERE id=".$knowledge["id"];
                //echo $sql."\n";
                $line = $db->createCommand($sql)->execute();
            }
        }

    }

    public function actionYoudao(){
        $db = Yii::$app->db;
        $sql = "SELECT id, word FROM {{%knowledge}} ORDER BY id ASC";

        $knowledges = $db->createCommand($sql)->queryAll();

        //print_r($knowledges);exit;
        foreach($knowledges as $knowledge){
            echo $knowledge['word']."\n";
            $text = Youdao::getText($knowledge['word']);
            //echo $text;

            if(empty($text)){
                echo "跳过"."\n";
                comtinue;
            }

            $data = [];
            $ipa = Youdao::getIPA($text);
            $ipa_en = isset($ipa[0]) ? $ipa[0] : '';
            $ipa_us = isset($ipa[1]) ? $ipa[1] : '';

            $trans = Youdao::getChineseTrans($text);

            $trans_str = implode('|', $trans);

            $data = [
                'ipa_en' => $ipa_en,
                'ipa_us' => $ipa_us,
                'trans' => $trans_str,
                'question' => implode(' ', $trans),
            ];

            print_r($data);

            $db = Yii::$app->db;

            $line = $db->createCommand()->update('{{%knowledge}}', $data, 'id='.$knowledge['id'])->execute();


        }

    }

    public function actionCet4(){
        $text = file_get_contents('d:/4.txt');
//        echo $text;

        $pattern = '/([a-zA-Z-\.]+?)[ ][a-zA-Z|(]+[\.]?/';
        $pattern = '/([a-zA-Z-\.]+?)[ ].*?\s/';
        $a = preg_match_all($pattern, $text, $matches);

        print_r($matches[1]);

        foreach($matches[1] as $w){
            file_put_contents('d:/4w.txt', $w."\n", FILE_APPEND);
        }
    }

    public function actionCet42(){
        $text = file_get_contents('d:/4w.txt');
//        echo $text;

        $pattern = '/([a-zA-Z-(.)]+?)\s+/';
        $a = preg_match_all($pattern, $text, $matches);

         //print_r($matches[1]);

        $words = $matches[1];
        shuffle($words);
        //print_r($words);exit;

        foreach($words as $w){
            file_put_contents('d:/4x.txt', $w."\n", FILE_APPEND);
        }

    }

    /**
     * 读文本并入库
     * @throws \yii\db\Exception
     */
    public function actionReadtodb($file){
        $text = file_get_contents($file);

        $pattern = '/([a-zA-Z-(.)]+?)\s+/';
        preg_match_all($pattern, $text, $matches);
        $db = Yii::$app->db;
        foreach($matches[1] as $word){
            echo $word."\n";
            $data = [
                'word'=>$word,
                'junior' => 1,
                'create_time' => time(),
            ];
            $db->createCommand()->insert('{{%word}}', $data)->execute();
        }
    }

    /**
     * 读文本并入库,存在则更新
     * @throws \yii\db\Exception
     */
    public function actionReadtodb2($file){
        $text = file_get_contents($file);

        $pattern = '/([a-zA-Z-(.)]+?)\s+/';
        preg_match_all($pattern, $text, $matches);
        $db = Yii::$app->db;
        foreach($matches[1] as $key => $word){
            //echo $key." ".$word."\n";

            $id = $db->createCommand("SELECT id FROM {{%word}} WHERE BINARY `word`='".$word."'")->queryScalar();

            if(intval($id) > 0){
                //echo "update:";
                $data = [
                    'id' => $id,
                    'ielts' => 1,
                    'update_time' => time(),
                ];
                $db->createCommand()->update('{{%word}}', $data, "id=".$id."")->execute();
            }else{
                //echo "insert:";
                $data = [
                    'word'=>$word,
                    'ielts' => 1,
                    'create_time' => time(),
                    'update_time' => time(),
                ];
                $db->createCommand()->insert('{{%word}}', $data)->execute();
            }


        }
    }


    /**
     * 文本提取单词，每行一个
     */
    public function actionMatchWord($file){
        $text = file_get_contents($file);
        $pattern = '/([a-zA-Z-\.]+).*?[\r\n]/';
        preg_match_all($pattern, $text, $matches);
        print_r($matches[1]);

        foreach($matches[1] as $w){
            file_put_contents($file."_", $w."\n", FILE_APPEND);
        }
    }

    /**
     * 去重
     */
    public function actionUnique($file){
        $text = file_get_contents($file);

        $pattern = '/([a-zA-Z-(.)]+?)\s+/';
        preg_match_all($pattern, $text, $matches);

        print_r($matches[1]);

        $words = $matches[1];
        $words = array_unique($words);
        $words = array_values($words);
        print_r($words);

        foreach($words as $w){
            file_put_contents($file.'_', $w."\n", FILE_APPEND);
        }

        exit;
    }

    public function actionTest(){
        $db = Yii::$app->db;
        $id =  $db->createCommand("SELECT `id` FROM {{%knowledge}} WHERE word='aa'")->queryAll();
        var_dump($id);
    }


    public function actionYs(){
        $text = file_get_contents('d:/ys.txt');
//        echo $text;

        $pattern = '/([a-zA-Z-\.]+?)[ ][a-zA-Z|(]+[\.]?/';
        $pattern = '/[\d ]*?([a-zA-Z-\.]+?)[ ]\[.*?[\r\n]/';
        $a = preg_match_all($pattern, $text, $matches);

        //print_r($matches[1]);

        foreach($matches[1] as $key => $w){
            file_put_contents('d:/ys3.txt', $w."\n", FILE_APPEND);
        }
    }



}
